//
//  HMNetworkTools.swift
//  
//
//  Created by tong dan on 16/3/2.
//
//

import UIKit
import AFNetworking


//请求方式的枚举
enum TDRequestMethod:String{
    case GET = "GET"
    case POST = "POST"
    
    
}

class TDNetworkTools: AFHTTPSessionManager {
    
    //全局访问点（单例）
    static let sharedTools:TDNetworkTools = {
        let tools = TDNetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    func request(method:TDRequestMethod = .GET,urlString:String,parameters:AnyObject?,finished:(response:AnyObject?,error:NSError?)->()){
    
    
        //定义请求成功的闭包
        let success = {(dataTasks:NSURLSessionTask,response:AnyObject?) -> Void in
            finished(response: response, error: nil)
            
        }
        
        //定义请求失败的闭包
        let failure = {(dataTask:NSURLSessionTask?, error:NSError) -> Void in
        finished(response: nil, error: error)
        }
        
        
        //根据请求方式，发送不同的请求
        if method == .GET {
        
        GET(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
            
        }else{
        
        POST(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
        
        }
    
    
    }
}




// MARK: - 首页微博数据请求
extension TDNetworkTools{
func loadStatuses(accessToken: String, finished:(response:AnyObject?,error:NSError?)->()){

let urlString = "https://api.weibo.com/2/statuses/friends_timeline.json"

let params = [
    
    "access_token":accessToken
    ]
    request(urlString: urlString, parameters: params, finished: finished)

    }
}
// MARK: - 登录获取AccessToken和个人信息
    extension TDNetworkTools{
        /// 请求AccessToken
        ///
        /// - parameter code:     授权码
        /// - parameter finished: 请求完成回调
    
        func loadAccessToken(code:String,finished:(response:AnyObject?,error:NSError?)->()) {
        
            //请求地址
            let urlString = "https://api.weibo.com/oauth2/access_token"
            
            
            //请求参数
            
            let params = [
                
                "client_id":WB_CLIENT_ID,
                "client_secret":WB_CLIENT_SECRET,
                "grant_type":"authorization_code",
                "code":code,
                "redirect_uri":WB_REDIRECT_URI
                
            ]

        request(.POST, urlString: urlString, parameters: params, finished: finished)
        
        }
    
        /// 加载个人信息
        ///
        /// - parameter accessToken: <#accessToken description#>
        /// - parameter uid:         <#uid description#>
        /// - parameter finished:    请求完成的闭包
        
        func loadUserInfo(accessToken:String, uid:String,finished:(response:AnyObject?,error:NSError?)->()){
        
            let urlString = "https://api.weibo.com/2/users/show.json"
            
            let params = [
                
                "access_token":accessToken,
                "uid":uid
                
            ]

        //发送请求
            request(urlString: urlString, parameters: params, finished: finished)
        
        }
    
    }
    

